import re



ccf = open("CC_base.cs", "r", 1)

cc0 = open("CC0_forward.cs", "w")
cc1 = open("CC1_forward.cs", "w")


#############################################################################


def transform_headline (headline):

    pattern = re.compile(r"int\s+firstIndex\,?\s*")
    changed_headline = pattern.sub("", headline)
    
    return changed_headline


def generate_forward (mtitle, margs):

    pattern = re.compile(r" \w+(\s*<[\w ,]+>)?\s*\(")
    matched = pattern.search(mtitle)
    
    if matched:
        print "\t\t" + mtitle[matched.start():matched.end()-1] + " ("+", ".join(margs)+")"
        f_exp = "            return CC." \
              + mtitle[matched.start()+1:matched.end()] \
              + "%%%, " + ", ".join(margs) \
              + "); \n"
    else:
        raise Exception, "Failed to analyze method line: " + mtitle 
    
    return f_exp
    
    
def out_lines (lines):

    lines0 = lines.replace("%%%", "0")
    lines1 = lines.replace("%%%", "1")
    
    cc0.write(lines0)
    cc1.write(lines1)
    
    


#############################################################################


print "==== CCx forwards: starting generate ===="


state = -1
depth = 0
mhead = ""
margs = []
mtitle = ""



for line in ccf:

    line1 = line.strip()

    if state == -1:   # before class
    
        if line1.find("partial") > 0:
            state = 0
            out_lines (line.replace("CC","CC%%%"))
        else:    
            out_lines (line)
    
    elif state == 0:  # in class
    
        if line1.startswith("#"):
            out_lines (line)
        elif line1.startswith("///"):
            state = 1
            margs = []
            mhead = '\n' + line
        elif line1.startswith("{"):
            out_lines (line)
        elif line1.startswith("}"):
            state = 9
            out_lines (line)
        elif line1 == "":
            out_lines ("")
        else:
            state = 8
            depth = 0    
    
    elif state == 1:  # in method-head comments

        if line1.startswith("///") and line1.find("firstIndex") > 0:
            xyz = 0 # really - do nothing
        elif line1.startswith("///") and line1.find("<param name") > 0:
            q1 = line1.find('"')
            q2 = line1.find('"', q1+1)
            arg = line1[q1+1:q2]
            margs += [arg]
        elif line1.startswith("["):
            mhead += line
        elif line1.startswith("/") or line1 == "":
            mhead += line
        elif line1.find("public") >= 0 and line1.find("static") >= 0:
            state = 2 
            depth = 0 
            mhead += transform_headline(line)
            mtitle = line1
            
    elif state == 2:  # inside the method
    
        if line1.startswith("{"):
            if depth == 0:
                mhead += line
            depth += 1
            if depth == 1:
                mhead += generate_forward(mtitle, margs)
        elif line1.startswith("}"):
            depth -= 1
            if depth == 0:
                mhead += line
                mhead += '\n'
                out_lines (mhead)
                state = 0
        else:
            if depth == 0:
                mhead += line        
       
    elif state == 8:  # private method   

        if line1.startswith("{"):
            depth += 1
        elif line1.startswith("}"):
            depth -= 1
            if depth == 0:
                state = 0
        elif line1.startswith("///"):
            state = 1
            mhead = '\n' + line
       
    elif state == 9:  # behind the class

        out_lines (line)

    
ccf.close()    
cc0.close()
cc1.close()


print "==== CCx forwards: generating finished ===="


exit(0)
   